﻿using shop.Bll;
using shop.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using unvell.ReoGrid;
using CommonTools;
using CommonTools.Controls;

namespace shop.Logi
{
    public partial class QueryStockoutForm : DockContent
    {

        shop.Bll.trade.StockOutBll bll = new Bll.trade.StockOutBll();


        public QueryStockoutForm()
        {
            InitializeComponent();

            this.box_begin.Value = DateTime.Today.AddDays(-10);
            this.box_end.Value = DateTime.Today;
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            turnPageBox1_PageChanged(1, turnPageBox1.PageSize);
        }


        private void turnPageBox1_PageChanged(int pageNo, int pageSize)
        {
            var begin = box_begin.Value.Value.Date;
            var end = box_end.Value.Value.Date.LastTimeOfDay();

            var pr = new OutStockItem()
            {
                shop_name = box_shop_name.Text.Trim(),
                logistics_no = box_logistics_no.Text.Trim(),
                trade_no = box_trade_no.Text.Trim(),
                outstock_no = box_outstock_no.Text.Trim(),
                goods_name = box_goods_name.Text.Trim(),
            };

            if ((end - begin).TotalDays > 30)
            {
                MessageBox.Show("日期范围不能超过30天", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            var res = WaitUI.Run(async () => await bll.QueryOutStock(begin, end, pageNo, pageSize, pr));

            if (res.IsOk)
            {
                new GridDataBuilder(this.grid1)
                    .UseData(res.Data)
                    .AutoCreateColumns()
                    .UseFilter()
                    .SetReadOnly(true)
                    .End();

                if (pageNo == 1)
                {
                    this.turnPageBox1.Reset(res.Rows);
                }

                MessageTip.ShowOk(this, "查询完成");
            }
            else
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            //var list = new List<stockout_order_query_tradeItem>();

            //list.AddRange(res.stockout_list);
            //if (res.stockout_list == null || res.stockout_list.Count == 0)
            //{
            //    //break;
            //}
            //}

            //new unvell.ReoGrid.GridDataBuilder(this.grid1)
            //    .UseData(list)
            //    //.AutoCreateColumns()
            //    .End();


            // 货品成本	货品总成本	单品支付金额	邮费	订单编号	原始单号	订单类型	仓库	店铺	商家编码	收货地区	物流公司	快递老板	客服备注	发货时间

            //var data = list
            //    .SelectMany(a => a.details_list
            //        .Select(c => new
            //        {
            //            a,
            //            c.spec_no,

            //        })
            //    )
            //    .Select(a => new
            //    {
            //        物流单号 = a.a.logistics_no,
            //        货品数量 = a.a.goods_count,
            //        预估邮资 = a.a.calc_post_cost,
            //        //货品成本=a.goods_total_cost,
            //        货品总成本 = a.a.goods_total_cost,
            //        //单品支付金额= 
            //        邮费 = a.a.post_fee,
            //        订单编号 = a.a.trade_no,
            //        原始单号 = a.a.src_trade_no,
            //        订单类型 = stockout_order_query_tradeResult.GetTradeTypeText(a.a.trade_type),
            //        仓库 = a.a.warehouse_name,
            //        店铺 = a.a.shop_name,
            //        商家编码 = a.spec_no,
            //        收货地区 = a.a.receiver_district,
            //        收货城市 = a.a.receiver_city,
            //        物流公司 = a.a.logistics_name,
            //        快递老板 = a.a.logistics_template_id,
            //        客服备注 = a.a.cs_remark,
            //        发货时间 = a.a.consign_time,
            //    })
            //    .ToList();

        }




        private void btn_syncData_Click(object sender, EventArgs e)
        {
            //var begin = box_begin.Value.Value.Date;
            //var end = box_end.Value.Value.Date.AddHours(23).AddMinutes(59).AddSeconds(59.998);
            //if (end > DateTime.Now.AddMinutes(-3))
            //{
            //    end = DateTime.Now.AddMinutes(-3);
            //}


            //if ((end - begin).TotalDays > 30)
            //{
            //    MessageBox.Show("日期范围不能超过30天", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}

            //try
            //{
            //    await WaitUI.RunAsync(async () =>
            //    {
            //        WaitUI.CanBeCanceled = true;

            //        int i = 0;
            //        WaitUI.BarStyle = ProgressBarStyle.Continuous;
            //        var c = 0;

            //        while (true)
            //        {
            //            if (WaitUI.IsCancellationRequested)
            //            {
            //                break;
            //            }

            //            //旺店通查询
            //            var res = await new QueryLogisticsBll().stockout_order_query_trade(begin, end, i);
            //            if (i == 0)
            //            {
            //                c = WaitUI.BarMaximum = res.total_count;
            //                WaitUI.WorkMessage = $"正在执行, 共{c}条, 当前已完成{WaitUI.BarValue}";
            //            }

            //            if (res.stockout_list == null || res.stockout_list.Count == 0)
            //            {
            //                break;
            //            }


            //            //保存到本地数据库
            //            await bll.SaveStockOutItems(res.stockout_list);

            //            WaitUI.BarValue += res.stockout_list.Count;
            //            WaitUI.WorkMessage = $"正在执行, 共{c}条, 当前已完成{WaitUI.BarValue}";

            //            i++;
            //        }
            //    });

            //}
            //catch(TaskCanceledException )
            //{
            //    MessageTip.ShowWarning(this, " 任务已取消");
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("系统错误", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
        

          

        }

        private void btn_syncLog_Click(object sender, EventArgs e)
        {
            var form = new SyncLogForm()
            { Flag= nameof(OutStockItem)};

            form.ShowDialog();

        }
    }
}
